package pub.guoxin.oops.manager.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import pub.guoxin.oops.annotation.InjectUser;
import pub.guoxin.oops.constants.OopsPathConstants;
import pub.guoxin.oops.manager.service.CommunicationService;
import pub.guoxin.oops.manager.vo.RealTimeCommunicationVo;
import pub.guoxin.oops.model.User;
import pub.guoxin.oops.vo.ResultVo;
import pub.guoxin.oops.vo.ResultVoBuilder;

/**
 * Created by guoxin on 17-11-5.
 */
@RestController
@PreAuthorize(value = "hasAnyRole('ADMIN')")
@RequestMapping(OopsPathConstants.COMMUNICATION_PATH)
public class CommunicationController {

    @Autowired
    private CommunicationService communicationService;
    @Autowired
    private ResultVoBuilder builder;

    @ApiOperation(value = "注册通讯模块", notes = "注册通讯模块（权限：公司管理员）")
    @PostMapping(value = "/{sn}/{groupId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo register(@InjectUser User user, @PathVariable String sn, @PathVariable String groupId) {
        return communicationService.register(user, sn, groupId);
    }

    @ApiOperation(value = "修改备注", notes = "修改备注（权限：公司管理员）")
    @PutMapping(value = "/{sn}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo modifyRemark(String sn, String remark){
        if (communicationService.modifyRemark(sn, remark)){
            return builder.buildSuccessResultInfo("修改成功");
        }
        return builder.buildBadRequestResultInfo("未找到该模块");
    }

    @ApiOperation(value = "按分组查询通讯模块", notes = "按分组查询通讯模块（用于通讯模块分页显示，权限：公司管理员）")
    @GetMapping(value = "/{groupId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultVo<Page<RealTimeCommunicationVo>> page(@InjectUser User user,
                                                        @PathVariable String groupId,
                                                        @RequestParam(required = false, defaultValue = "1") Integer pageNumber,
                                                        @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        Page<RealTimeCommunicationVo> page = communicationService.page(user, groupId, pageNumber, pageSize);
        return builder.buildSuccessResultInfo(page);
    }

}
